package com.ruoyi.project.beautyflower.contract.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ai.advance.cmp.api.client.bean.ResponseBean;
import com.alibaba.fastjson.JSON;
import com.ruoyi.project.app.interfaces.MoneyTranferInterface;
import com.ruoyi.project.beautyflower.BfUtil;
import com.ruoyi.project.beautyflower.member.mapper.MemberMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.beautyflower.contract.domain.Contract;
import com.ruoyi.project.beautyflower.contract.service.IContractService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 借款合同 信息操作处理
 *
 * @author lxs
 * @date 2018-10-11
 */
@Controller
@RequestMapping("/beautyflower/contract")
public class ContractController extends BaseController implements MoneyTranferInterface {
    private String prefix = "beautyflower/contract";

    @Autowired
    private IContractService contractService;
    @Autowired
    private MemberMapper memberMapper;

    @RequiresPermissions("beautyflower:contract:view")
    @GetMapping()
    public String contract() {
        return prefix + "/contract";
    }

    /**
     * 查询借款合同列表
     */
    @RequiresPermissions("beautyflower:contract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Contract contract) {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 新增借款合同
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存借款合同
     */
    @RequiresPermissions("beautyflower:contract:add")
    @Log(title = "借款合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Contract contract) {
        return toAjax(contractService.insertContract(contract));
    }

    /**
     * 修改借款合同
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Contract contract = contractService.selectContractById(id);
        mmap.put("contract", contract);
        return prefix + "/edit";
    }

    /**
     * 修改保存借款合同
     */
    @RequiresPermissions("beautyflower:contract:edit")
    @Log(title = "借款合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Contract contract) {
        return toAjax(contractService.updateContract(contract));
    }

    /**
     * 删除借款合同
     */
    @RequiresPermissions("beautyflower:contract:remove")
    @Log(title = "借款合同", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(contractService.deleteContractByIds(ids));
    }

    /**
     * 修改借款合同
     */
    @GetMapping("/index")
    public String edit(@RequestParam Map<String, Object> params, Integer uid, ModelMap mmap) {
        // params:{ uid, trialAmount, installments }
        mmap.putAll(params);
        mmap.put("currentTime", new Date());
        mmap.put("member", memberMapper.selectMemberById(uid));
        mmap.put("repaymentList", BfUtil.trailRepaymentSchedule(params).getData());
        mmap.put("cn_trialAmount", moneyToCN(params.get("trialAmount")));
        System.out.println("contract-index-params:" + JSON.toJSONString(mmap));
        return prefix + "/index";
    }
}
